package ar.com.caeycae.view;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import ar.com.caeycae.data.Concepto;
import ar.com.caeycae.service.ConceptoService;

@ManagedBean(name="conceptoView")
@ViewScoped
public class ConceptoView implements Serializable{

	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(ConceptoView.class);
	
	private static final String LISTAR = "LISTAR";
	private static final String CREAR = "CREAR";
	private static final String EDITAR = "EDITAR";
	
	@ManagedProperty("#{conceptoService}")
	private ConceptoService conceptoService;

	private List<Concepto> conceptos;
		
	private Concepto concepto;
	
	private String modo;
	
	@PostConstruct
	public void init() {
		this.modo = LISTAR;
		this.concepto = new Concepto();
		this.conceptos = conceptoService.listar();
	}
	
	public ConceptoService getConceptoService() {
		return conceptoService;
	}

	public void setConceptoService(ConceptoService conceptoService) {
		this.conceptoService = conceptoService;
	}

	public String getModo() {
		return modo;
	}

	public void setModo(String modo) {
		this.modo = modo;
		if(LISTAR.equalsIgnoreCase(modo))
			this.conceptos = conceptoService.listar();
	}

	public Concepto getConcepto() {
		return concepto;
	}

	public void setConcepto(Concepto concepto) {
		this.concepto = concepto;
	}

	public List<Concepto> getConceptos() {
		return conceptos;
	}

	public void setConceptos(List<Concepto> conceptos) {
		this.conceptos = conceptos;
	}

	public void crear(){
		this.concepto = new Concepto();
		setModo(CREAR);
	}
	
	public void agregar() {
		LOGGER.debug("Agregando concepto");
		concepto = conceptoService.agregar(concepto);
		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage("El concepto "+this.concepto+" fue agregado."));
		concepto = new Concepto();
		setModo(LISTAR);
	}
	
	public void editar(Concepto concepto){
		this.concepto = concepto;
		setModo(EDITAR);
	}
	
	public void actualizar() {
		conceptoService.actualizar(concepto);
		setModo(LISTAR);
	}

	public void cancelar() {
		concepto = new Concepto();
		setModo(LISTAR);
	}
	
	public void borrar(Concepto concepto) {
		conceptoService.borrar(concepto);
		setModo(LISTAR);
	}
}
